<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="webpack简单使用"><meta name="keywords" content="webpack"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>webpack简单使用 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E9%A6%96%E5%85%88%E8%BF%9B%E8%A1%8C%E4%B8%80%E4%B8%AAwebpack%E7%9A%84%E5%AE%89%E8%A3%85"><span class="toc-number">1.</span> <span class="toc-text">1.首先进行一个webpack的安装</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-webpack-config-js"><span class="toc-number">2.</span> <span class="toc-text">2.webpack.config.js</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-loader"><span class="toc-number">3.</span> <span class="toc-text">3.loader</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-plugins"><span class="toc-number">4.</span> <span class="toc-text">4.plugins</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-%E7%83%AD%E6%9B%B4%E6%96%B0"><span class="toc-number">5.</span> <span class="toc-text">5.热更新</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">webpack简单使用</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-09-07</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">473</span><span class="post-meta__separator">|</span><span>阅读时长: 1 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>就是在菜鸟的一个简单学习和使用，并没有深入</p>
<h5 id="1-首先进行一个webpack的安装"><a href="#1-首先进行一个webpack的安装" class="headerlink" title="1.首先进行一个webpack的安装"></a>1.首先进行一个webpack的安装</h5><figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">cnpm install webpack webpack-cli -g</span><br></pre></td></tr></table></figure>

<p>打包使用</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack filename -o targetname --mode=developmention/production  </span><br><span class="line">webpack ./src/index.js -o ./dist/bundle.js --mode=production //例</span><br></pre></td></tr></table></figure>

<p>开发环境里面会包含注释这些，会更详细。生产环境或对代码进行压缩，删除注释，代码混淆这些，大小也更小。</p>
<h5 id="2-webpack-config-js"><a href="#2-webpack-config-js" class="headerlink" title="2.webpack.config.js"></a>2.webpack.config.js</h5><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);<span class="comment">//引入node的path</span></span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>); <span class="comment">// 通过 npm 安装，生成一个html文件，包含bundle.js</span></span><br><span class="line"><span class="keyword">const</span> webpack = <span class="built_in">require</span>(<span class="string">&#x27;webpack&#x27;</span>); <span class="comment">// 用于访问内置插件</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="attr">entry</span>: <span class="string">&quot;./main.js&quot;</span>, <span class="comment">//入口文件</span></span><br><span class="line">    <span class="attr">output</span>: &#123;</span><br><span class="line">        <span class="attr">path</span>: path.resolve(__dirname, <span class="string">&#x27;dist&#x27;</span>),, <span class="comment">//输出路径，默认为dist文件夹</span></span><br><span class="line">        filename: <span class="string">&quot;bundle.js&quot;</span> <span class="comment">//输出文件名</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">mode</span>:<span class="string">&#x27;develpoment&#x27;</span>,<span class="comment">//模式</span></span><br><span class="line">    <span class="comment">//loader配置</span></span><br><span class="line">    <span class="attr">module</span>: &#123;</span><br><span class="line">        <span class="comment">//先进行文件匹配，在进行处理</span></span><br><span class="line">        <span class="attr">loaders</span>: [</span><br><span class="line">            &#123; <span class="attr">test</span>: <span class="regexp">/\.css$/</span>,</span><br><span class="line">              use:[</span><br><span class="line">                  <span class="comment">//use的数组内容loader是从下到上，逆序执行</span></span><br><span class="line">                  <span class="string">&quot;style-loader&quot;</span>, <span class="comment">//将js的样式内容插入到style标签里面</span></span><br><span class="line">                  <span class="string">&quot;css.loader&quot;</span> <span class="comment">//将css文件转换成js文件</span></span><br><span class="line">              ]</span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//插件配置</span></span><br><span class="line">    <span class="attr">plugins</span>: [</span><br><span class="line">    <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">        <span class="attr">template</span>: <span class="string">&#x27;./src/index.html&#x27;</span></span><br><span class="line">    &#125;)</span><br><span class="line">  ],</span><br><span class="line">    <span class="attr">devServer</span>:&#123;</span><br><span class="line">        <span class="attr">contentBase</span>:path.resolve(__pathname,<span class="string">&quot;dist&quot;</span>), <span class="comment">//目标路径</span></span><br><span class="line">        <span class="attr">compress</span>:<span class="literal">true</span>, <span class="comment">//自动刷新</span></span><br><span class="line">        <span class="attr">port</span>:<span class="number">3000</span>  <span class="comment">//端口号</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>之后直接在命令行使用</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">webpack</span><br></pre></td></tr></table></figure>

<p>就可以进行打包</p>
<h5 id="3-loader"><a href="#3-loader" class="headerlink" title="3.loader"></a>3.loader</h5><p><em>loader</em> 让 webpack 能够去处理那些非 JavaScript 文件（webpack 自身只理解 JavaScript）。</p>
<h5 id="4-plugins"><a href="#4-plugins" class="headerlink" title="4.plugins"></a>4.plugins</h5><p>使用一个插件，你只需要 <code>require()</code> 它，然后把它添加到 <code>plugins</code> 数组中.在一个配置文件中因为不同目的而多次使用同一个插件，这时需要通过使用 <code>new</code> 操作符来创建它的一个实例</p>
<h5 id="5-热更新"><a href="#5-热更新" class="headerlink" title="5.热更新"></a>5.热更新</h5><p>可以在进行修改之后，马上看见webpack后的效果，不需要再进行webpack命令。使用devServe，需要安装依赖。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">cnpm install webpack-dev-server -g</span><br><span class="line">webpack-dev-server <span class="comment">//进行打开</span></span><br></pre></td></tr></table></figure>

</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2020/09/07/webpack简单使用/">https://chengsong.info/2020/09/07/webpack简单使用/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/webpack/">webpack</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/09/10/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80/"><i class="fa fa-chevron-left">  </i><span>计算机网络基础</span></a></div><div class="next-post pull-right"><a href="/2020/09/07/yarn%E5%92%8Cnpm/"><span>yarn和npm</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>